#include "../lvgl/examples/lv_examples.h"
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "dir.h"
#include <string.h>


static P_Node_t FileList ;
lv_obj_t * obj;
static P_Node_t Tmp ;
static lv_obj_t * img ;
static lv_obj_t * gif ;

void BtnTouch (lv_event_t * e) 
{
	// 获取用户点解的按键是哪一个
	lv_obj_t * label = lv_event_get_user_data(e);

	char * msg = lv_label_get_text( label );

	char NewPath[256]={0} ;

	if(msg[0] == 'P')
	{
		printf("上一张..\n");
		Tmp = Tmp->Prev ;
		if (Tmp == FileList)
		{
			Tmp = Tmp->Prev ;
		}
	}
	else{
		Tmp = Tmp->Next ;
		if (Tmp == FileList)
		{
			Tmp = Tmp->Next ;
		}
		printf("下一张..\n");
	}

	snprintf( NewPath , sizeof(NewPath) , "A:%s" ,  Tmp->Data.FilePath );
	printf("NewPaht:%s\n" , NewPath );

	if (Tmp->Data.Type == 'g')
	{
		/* GIF处理 */
		if (gif == NULL)
		{
			gif = lv_gif_create( obj );
			lv_obj_set_align( gif , LV_ALIGN_TOP_MID );
		}
		lv_gif_set_src( gif , NewPath );
	}
	else{
		/* 静态图 */
		if (gif != NULL)
		{
			lv_obj_del(gif);
			gif = NULL ;
		}
		lv_img_set_src( img , NewPath );
	}

			
}


void InitUi()
{

	//////////////////初始化图像显示区///////////////////////
	// 创建一个基础对象（矩形 ，作为图像显示区）
	obj = lv_obj_create( lv_scr_act() );
	lv_obj_set_size( obj , 780 , 400 );
	lv_obj_align(obj , LV_ALIGN_TOP_MID , 0 , 10 ) ;


	// 初始化样式
	static lv_style_t style;  // 定义一个新的样式类型
	lv_style_init(&style); // 初始化新的样式

	// 设置圆角、透明度、背景色、阴影等样式属性
	lv_style_set_radius(&style, 5);
	lv_style_set_bg_opa(&style, LV_OPA_COVER);
	lv_style_set_bg_color(&style, lv_palette_lighten(LV_PALETTE_GREY, 1));
	lv_style_set_shadow_width(&style, 55);
	lv_style_set_shadow_color(&style, lv_palette_main(LV_PALETTE_BLUE));

	// 将样式添加到obj
	lv_obj_add_style(obj, &style, 0);


	//////////////////初始化按钮///////////////////////
	lv_obj_t * btnPrev = lv_btn_create(lv_scr_act());
	lv_obj_set_size( btnPrev , 100 , 60 );
	lv_obj_align(btnPrev , LV_ALIGN_BOTTOM_MID , -200 , -10 ) ;

	lv_obj_t * labelPrev = lv_label_create( btnPrev );
	lv_label_set_text(labelPrev , "Prev") ;


	lv_obj_t * btnNext = lv_btn_create(lv_scr_act());
	lv_obj_set_size( btnNext , 100 , 60 );
	lv_obj_align(btnNext , LV_ALIGN_BOTTOM_MID , 200 , -10 ) ;

	lv_obj_t * labelNext = lv_label_create( btnNext );
	lv_label_set_text(labelNext , "Next") ;

	// 注册点击事件的响应函数
	lv_obj_add_event_cb(btnPrev , BtnTouch , LV_EVENT_CLICKED , labelPrev ) ;
	lv_obj_add_event_cb(btnNext , BtnTouch , LV_EVENT_CLICKED , labelNext ) ;

	// 创建静态图像对象
	img = lv_img_create( obj );
	gif = lv_gif_create( obj );

	lv_obj_set_align( gif , LV_ALIGN_TOP_MID );
	lv_obj_set_align( img , LV_ALIGN_TOP_MID );




}


void myMain( void )
{
	// 初始化链表
	FileList = InitNewNode( NULL );
	if (FileList == NULL)
	{
		perror("list Init Error");
	}

	// 让tmp指向第一个有效数据
	Tmp = FileList->Next ;


	// 目录检索
	int retVal = DirRetrieval( "/jakl/img/" , ".bmp" , FileList );
	printf("检索到%d个bmp文件..\n" , retVal);


	retVal = DirRetrieval( "/jakl/img/" , ".jpg" , FileList );
	printf("检索到%d个jpg文件..\n" , retVal);

	retVal = DirRetrieval( "/jakl/img/" , ".png" , FileList );
	printf("检索到%d个png文件..\n" , retVal);

	retVal = DirRetrieval( "/jakl/img/" , ".gif" , FileList );
	printf("检索到%d个gif文件..\n" , retVal);


	// 界面初始化
	InitUi();
	


}